package com.nicefact.mailmonitor.dao;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.stereotype.Component;

import com.google.appengine.api.datastore.Key;
import com.nicefact.mailmonitor.entity.AppUser;
import com.nicefact.mailmonitor.vo.GridRequest;

@Component
public class AppUserDao extends AbstractDao<AppUser>{
	
	public AppUser get(String email) {
		PersistenceManager pm = super.getPersistenceManager();		
		Query query = pm.newQuery(AppUser.class);
		query.declareParameters("String userMail");
		query.setFilter("email==userMail");
		query.setRange(0,1);
		AppUser obj = null;
		try {
			List<AppUser> results = (List<AppUser>) query.execute(email);
			if (!results.isEmpty()) {
				obj = pm.detachCopy(results.get(0));
			}
		} finally {
			query.closeAll();
			pm.close();
		}
		
		
		return obj;
	}
	
	public List<AppUser> getForAdmin(String adminEmail) {
		PersistenceManager pm = super.getPersistenceManager();		
		Query query = pm.newQuery(AppUser.class);
		query.declareParameters("String userMail");
		query.setFilter("createdBy==userMail");		
		List<AppUser> objectList = new ArrayList<AppUser>();
		int start = 0;
		int max = 800;
		try{
			while (true) {
				query.setRange(start, start + max);
				List<AppUser> results = (List<AppUser>) query.execute(adminEmail);
				if (results.isEmpty()) {
					break;
				} else {
					objectList.addAll(pm.detachCopyAll(results));
					start += max;
				}
			}
		}finally{
			query.closeAll();
			pm.close();
		}
		return objectList;
		
	}
	
	public AppUser getByKey(Key key) {
		return super.get(AppUser.class, key);
	}
	
	public AppUser save(AppUser appUser) {
		AppUser	obj = super.create(appUser);
		return obj;
	}
	
	public List<AppUser> getAll(){
		PersistenceManager pm = getPersistenceManager();
		Query query =  pm.newQuery(AppUser.class);
		List<AppUser> objectList = new ArrayList<AppUser>();
		int start = 0;
		int max = 800;
		try{
			while (true) {
				query.setRange(start, start + max);
				List<AppUser> results = (List<AppUser>) query.execute();
				if (results.isEmpty()) {
					break;
				} else {
					objectList.addAll(pm.detachCopyAll(results));
					start += max;
				}
			}
		}finally{
			query.closeAll();
			pm.close();
		}
		return objectList;
	}

	public List<AppUser> getAllUsers(String createdBy) {
		PersistenceManager pm = getPersistenceManager();
		Query query =  pm.newQuery(AppUser.class);
		List<AppUser> objectList = new ArrayList<AppUser>();
		query.setFilter("createdBy==email");
		query.declareParameters("String email");
		int start = 0;
		int max = 800;
		try{
			while (true) {
				query.setRange(start, start + max);
				List<AppUser> results = (List<AppUser>) query.execute(createdBy);
				if (results.isEmpty()) {
					break;
				} else {
					objectList.addAll(pm.detachCopyAll(results));
					start += max;
				}
			}
		}finally{
			query.closeAll();
			pm.close();
		}
		return objectList;
	}

	public AppUser get(String userEmail, String adminEmail) {
		PersistenceManager pm = super.getPersistenceManager();		
		Query query = pm.newQuery(AppUser.class);
		query.declareParameters("String userMail,String adminEmail");
		query.setFilter("email==userMail");
		query.setFilter("createdBy==adminEmail");
		query.setRange(0,1);
		AppUser obj = null;
		try {
			List<AppUser> results = (List<AppUser>) query.execute(userEmail,adminEmail);
			if (!results.isEmpty()) {
				obj = pm.detachCopy(results.get(0));
			}
		} finally {
			query.closeAll();
			pm.close();
		}
		
		
		return obj;
	}

	public List<AppUser> searchUsers(GridRequest dataCriteria, String email) {
		PersistenceManager pm = getPersistenceManager();
		Query query =  pm.newQuery(AppUser.class);
		List<AppUser> objectList = new ArrayList<AppUser>();
		query.setFilter("createdBy==adminEmail");
		query.declareParameters("String adminEmail");
		query.setOrdering(dataCriteria.getSortInfo().getSortField() + " " + dataCriteria.getSortInfo().getSortOrder());
		int pageNo = dataCriteria.getPaginationInfo().getPageNumber();
		int max = dataCriteria.getPaginationInfo().getRecordsPerPage();
		int start = (pageNo-1) * max;
		try{
				query.setRange(start, start + max);
				List<AppUser> results = (List<AppUser>) query.execute(email);
				if (!results.isEmpty()) {
					objectList.addAll(results);
			}
		}finally{
			query.closeAll();
			pm.close();
		}
		return objectList;
	}
		
		
		

}
